package com.thread.consumeandproduct;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MCLJETMain {

    public static ExecutorService executorService = Executors.newFixedThreadPool(2);
    public static final String ADD_MUTIL_A = "a";
    public static final String REC_MUTIL_R = "r";
    public static final String INSERT_MUTIL_I = "i";
    public static final String EXIT_MUTIL_EXIT = "exit";
    public static final String COLLECTION_MUTIL_C = "c";

    public static void main(String[] args) {
        boolean flag = true;


        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(is);
        while (flag) {
            try {
                Thread.sleep(2000);
                String name = br.readLine();
                System.out.println(name);
                if (name != null && name.startsWith(ADD_MUTIL_A)) {
                    for (int i = 1; i < 5; i++) {
                        String content = name.substring(ADD_MUTIL_A.length());
                        ProductJetBean productJetBean = new ProductJetBean(content);
                        productJetBean.setOnJetFinishListener(new OnJetFinishListener() {
                            @Override
                            public void timeOut(ProductJetBean productJetBean) {
                                System.out.println(productJetBean.getContent() + " timeout");
                            }

                            @Override
                            public void finish(ProductJetBean productJetBean) {
                                System.out.println(productJetBean.getContent() + " finish");
                            }
                        });
                        JetComManager.getInstance().productMutil(productJetBean);
                    }

                } else if (name != null && name.startsWith(REC_MUTIL_R)) {
                    String content = name.substring(REC_MUTIL_R.length());
                    JetComManager.getInstance().receiveJet(content);
                } else if (name != null && name.startsWith(INSERT_MUTIL_I)) {
                    String content = name.substring(INSERT_MUTIL_I.length());
                    ProductJetBean productJetBean = new ProductJetBean(content);
                    productJetBean.setOnJetFinishListener(new OnJetFinishListener() {
                        @Override
                        public void timeOut(ProductJetBean productJetBean) {
                            System.out.println(productJetBean.getContent() + " timeout");
                        }

                        @Override
                        public void finish(ProductJetBean productJetBean) {
                            System.out.println(productJetBean.getContent() + " finish");
                        }
                    });
                    boolean isAdded = JetComManager.getInstance().productMutexSingle(productJetBean);
                    if (isAdded) {
                        System.out.println("addSingleProduct");
                    } else {
                        System.out.println("no addSingleProduct");
                    }
                } else if (name != null && name.startsWith(EXIT_MUTIL_EXIT)) {
                    executorService.shutdownNow();
                    flag = false;
                } else if (name != null && name.startsWith(COLLECTION_MUTIL_C)) {
                    List<ProductJetBean> productJetBeanList = new ArrayList<>();
                    productJetBeanList.add(new ProductJetBean("1"));
                    productJetBeanList.add(new ProductJetBean("2"));
                    if ( !JetComManager.getInstance().productContinueList(productJetBeanList, true)){
                        System.out.println("unenable add");
                    }


                }
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
